package com.seednia.module.core.user.service.ws;

import java.util.List;

import javax.jws.WebService;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seednia.foundation.framework.dto.QueryParams;
import com.seednia.foundation.framework.service.AbstractWebService;
import com.seednia.foundation.framework.service.RemoteException;
import com.seednia.foundation.framework.service.RequestContext;
import com.seednia.foundation.framework.service.ServiceContainer;
import com.seednia.foundation.framework.service.ServiceException;
import com.seednia.module.core.project.service.ws.ProjectSoapService;
import com.seednia.module.core.resource.dto.MediaDto;
import com.seednia.module.core.user.dto.UserCriteria;
import com.seednia.module.core.user.dto.UserDto;
import com.seednia.module.core.user.service.IUserService;

/**
 * User service exposing SOAP protocol
 * @author Young Suk Ahn
 *
 */
@WebService(
		serviceName="UserService",
		endpointInterface = "com.seednia.module.core.user.service.ws.IUserWebService")
public class UserSoapService extends AbstractWebService implements IUserWebService {
	
	private IUserService userService = null;
	
	private static final Log logger = LogFactory.getLog(ProjectSoapService.class);
	
	public UserSoapService() 
	{
	}

	@Override
	public UserDto getUser(String accessToken, String xid) 
			throws RemoteException
	{
		UserDto result = null;
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			UserCriteria criteria = new UserCriteria();
			super.setCriteriaXid(criteria, xid);
			result = getUserService().getUser(requestContext, criteria);
		} catch (ServiceException e) {
			throw new RemoteException(e);
		}
        return result;
    }
	
	@Override
    public List<UserDto> getUserList(String accessToken, UserCriteria criteria
    		, QueryParams params) 
    throws RemoteException
    {
		List<UserDto> result = null;
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			result = getUserService().getUserList(requestContext, criteria, params);
		} catch (ServiceException e) {
			throw new RemoteException(e);
		}
		
        return result;
    }
	
	@Override
    public Long insertUser(String accessToken, UserDto record) 
    		throws RemoteException
    {
		Long retval = null;
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			retval = getUserService().insertUser(requestContext, record);
		} catch (ServiceException e) {
			throw new RemoteException(e);
		}

		return retval;
    }
	
	@Override
    public int updateUser(String accessToken, UserDto record) 
    		throws RemoteException
    {
		
    	int result = 0;
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			result = getUserService().updateUser(requestContext, record);
		} catch (ServiceException e) {
			throw new RemoteException(e);
		}
		
        return result;
    }

	
	@Override
    public int deleteUser(String accessToken, UserCriteria criteria) 
    		throws RemoteException
	{
		int result = 0;
		if (criteria.getSid() != null) {
			try {
				RequestContext requestContext = createRequestContext(accessToken);
				result = getUserService().deleteUser(requestContext, criteria);
			} catch (ServiceException e) {
				throw new RemoteException(e);
			}
		}

		return result;
    }
	
	@Override	
	public int updateActiveProject(String accessToken
			, long userSid, long activeProjectSid) 
	{
		int result = 0;
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			result = getUserService().updateActiveProject(requestContext, userSid, activeProjectSid);
			
		} catch (ServiceException e) {
			throw new WebApplicationException(e);
		}
        return result;
    }
	
	@Override
	public int updateUserProfileImage(String accessToken, long userSid, String which, MediaDto media) throws RemoteException
	{
		try {
			RequestContext requestContext = createRequestContext(accessToken);
			return getUserService().updateUserProfileImage(requestContext, userSid, which, media);
		} catch (ServiceException e) {
			logger.error("Exception at UserSoapService.updateUserProfileImage", e);
			throw new WebApplicationException(e);
		} 
	}	

	public IUserService getUserService() {
		if (userService == null) {
			//ServiceContainer.getInstance().getById(IUserService.class.getName());
			userService = (IUserService)ServiceContainer.getInstance().getByType(IUserService.class);
		}
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}


}
